import os
from pathlib import Path

def count_excel_files(directory):
    # 支持的Excel文件扩展名
    excel_extensions = ('.xlsx', '.xls', '.xlsm', '.xlsb')
    
    # 存储每个文件夹的Excel文件信息
    folder_stats = {}
    total_excel_files = 0
    
    # 遍历目录
    for root, dirs, files in os.walk(directory):
        excel_files = [f for f in files if f.lower().endswith(excel_extensions)]
        if excel_files:
            # 使用相对路径作为键
            rel_path = os.path.relpath(root, directory)
            folder_stats[rel_path] = {
                'count': len(excel_files),
                'files': excel_files
            }
            total_excel_files += len(excel_files)
    
    # 打印统计结果
    print(f"\n总共找到 {total_excel_files} 个Excel文件\n")
    print("各文件夹统计信息：")
    print("-" * 50)
    
    for folder, stats in folder_stats.items():
        print(f"\n文件夹: {folder}")
        print(f"Excel文件数量: {stats['count']}")
        print("包含的Excel文件:")
        for file in stats['files']:
            print(f"  - {file}")
        print("-" * 50)

if __name__ == "__main__":
    # 获取用户输入的目录路径
    directory = input("请输入要统计的文件夹路径: ")
    
    # 检查目录是否存在
    if not os.path.exists(directory):
        print("错误：指定的目录不存在！")
    else:
        count_excel_files(directory)